package com.example.xViewChat.UI;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import com.example.xViewChat.Bean.IntentBean;
import com.example.xViewChat.NettyClient.PushManager;
import com.example.xViewChat.R;
import com.example.xViewChat.UI.Adapter.MsgListViewAdapter;
import io.ganguo.chat.route.biz.entity.Message;

import java.util.ArrayList;

/**
 * Created by hasee on 2016-04-10.
 */
public class MsgActivity extends EventListenerActivity implements View.OnClickListener {
    private ArrayList<Message> list;

    protected ListView chatListView;
    int currentPage = 1;
    protected MsgListViewAdapter adapter;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_system_chat);

        initView();
    }

    private void initView() {
        list = new ArrayList<Message>();

        chatListView = (ListView) findViewById(R.id.chat_list);
        findViewById(R.id.TOP_BACK_BUTTON).setOnClickListener(this);
        findViewById(R.id.TOP_BACK_BUTTON).setVisibility(View.VISIBLE);
        ((TextView) findViewById(R.id.TOP_BACK_BUTTON)).setText("登录");
        ((TextView) findViewById(R.id.TITLE_TEXT)).setText("系统消息");
        ((TextView) findViewById(R.id.account)).setText(this.getIntent().getStringExtra("account"));

        adapter = new MsgListViewAdapter(this, list);
        chatListView.setAdapter(adapter);

        showToask("登录成功，请通过后台页面发送消息吧^_^");
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onMessageReceived(IntentBean intentBean) {
        //这里应该是接受到了消息，然后进行显示。

        System.out.println("receive the message！");
        MediaPlayer.create(this, R.raw.classic).start();

        list.add((Message) intentBean.getEntity());
        adapter.notifyDataSetChanged();
        chatListView.setSelection(chatListView.getTop());
    }

    @Override
    public void onConnectionSucceed() {
        System.out.println("������onConnectionSucceed����Ҫ����loginActivity");
        Intent intent = new Intent(MsgActivity.this, LoginAcitivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void onBackPressed() {
        finish();
        PushManager.destory(this);
    }
}
